package omq.screen;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import constant.OmqTesterClientConst;

import omq.api.logger.*;
import omq.panel.OmqPanel;

public abstract class DefaultScreen extends JFrame implements ActionListener{

	protected Preferences myPreferences = null;
	protected JPanel mainPanel;
	protected JPanel lowerPanel;
	protected JPanel upperPanel;
	
    public DefaultScreen(String title) throws HeadlessException {
        super(title);
        initialize();
    }

    public DefaultScreen(){
        super();
        initialize();
    }

    public void initialize() {
    	OMQLogger.getInstance().info("starting OnlineMQ tester client version "+OmqTesterClientConst.CLIENT_VERSION);
    	myPreferences = Preferences.userNodeForPackage(OmqTesterMainScreen.class);
    	getContentPane().setBackground(Color.white); 
    	mainPanel = new OmqPanel(new BorderLayout());
		lowerPanel = new OmqPanel();
		upperPanel = new OmqPanel(new GridLayout(0,2,5,5));
		
		addMenu();
		
		upperPanel.add(new JLabel(createImageIcon("../../images/omqLogo.gif","aaa")));
		upperPanel.add(new JLabel("Online MQ tester client"));
		upperPanel.setPreferredSize(new Dimension(650,70));
		
		lowerPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Activity attributes"),BorderFactory.createEmptyBorder(5,5,5,5)));
		lowerPanel.setPreferredSize(new Dimension(650,480));
		
		mainPanel.add(upperPanel,BorderLayout.NORTH);
		mainPanel.add(lowerPanel,BorderLayout.CENTER);

        setPanels();
        setContentPane(mainPanel);
        //Creating the default size of the screen.
		this.setSize(650,550);
		this.setResizable(false);
		this.setIconImage(createImageIcon("../../images/favicon.gif","aaa").getImage());
	}
    
    protected abstract void setPanels();
    protected abstract void addMenu();

	/**************************************
	 * 		Inner components getters	  *
	 **************************************/
    
    /** Returns an ImageIcon, or null if the path was invalid. */
    public static ImageIcon createImageIcon(String path,String description) {
        java.net.URL imgURL = DefaultScreen.class.getResource(path);
        OMQLogger.getInstance().debug("Creating image with : " + imgURL);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            OMQLogger.getInstance().error("Couldn't find file: " + path);
            return null;
        }
    }


}